package com.yugao.fintech.antelope.pay.payment.channel;

import com.yugao.fintech.antelope.base.model.constants.ServiceCodeCons;
import com.yugao.fintech.antelope.pay.common.service.PaySysConfigService;
import com.yugao.fintech.antelope.pay.constant.IfCodeEnum;
import com.yugao.fintech.antelope.pay.payment.service.ConfigContextQueryService;
import com.yugao.fintech.framework.assistant.utils.exception.Assert;

import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 退款接口抽象类
 *

 */
public abstract class AbstractRefundService implements IRefundService {

    @Autowired
    protected PaySysConfigService paySysConfigService;
    @Autowired
    protected ConfigContextQueryService configContextQueryService;

    private static final Map<IfCodeEnum, IRefundService> serviceMap = new ConcurrentHashMap<>();

    @PostConstruct
    public void abstractRefundServiceInit() {
        IfCodeEnum ifCode = this.getIfCode();
        Assert.notNull(ifCode, "ifCode is null, class: " + this.getClass().getName());
        serviceMap.put(ifCode, this);
    }

    public static IRefundService getService(IfCodeEnum ifCodeEnum) {
        Assert.notNull(ifCodeEnum, "ifCodeEnum is null");
        return serviceMap.get(ifCodeEnum);
    }

    public static IRefundService getService(String ifCode) {
        return serviceMap.get(IfCodeEnum.ofByCode(ifCode));
    }

    protected String getNotifyUrl() {
        return paySysConfigService.getDBApplicationConfig().getPaySiteUrl()
                + "/api/" + ServiceCodeCons.PAY_SERVICE + "/refund/notify/" + getIfCode().getCode();
    }

    protected String getNotifyUrl(String refundOrderId) {
        return paySysConfigService.getDBApplicationConfig().getPaySiteUrl()
                + "/api/" + ServiceCodeCons.PAY_SERVICE + "/refund/notify/" + getIfCode().getCode() + "/" + refundOrderId;
    }

}
